package com.hopu.v1.controller.front.question;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hopu.v1.pojo.common.R;
import com.hopu.v1.pojo.question.QuestionsComments;
import com.hopu.v1.service.front.question.FQuestionsCommentsService;
import com.hopu.v1.service.front.question.FQuestionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/question/comment")
public class FQuestionsCommentsController {
    //问题评论
    @Autowired
    private FQuestionsCommentsService fQuestionsCommentsService;

    //题目下的父类评论
    @GetMapping("/list")
    public R list(Integer questionId) {
        LambdaQueryWrapper<QuestionsComments> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QuestionsComments::getQuestionId, questionId);
        wrapper.eq(QuestionsComments::getPid, 0);
        return R.ok(fQuestionsCommentsService.list(wrapper));
    }

    //父类评论下的子评论
    @GetMapping("/commentList")
    public R commentList(Integer commentId) {
        LambdaQueryWrapper<QuestionsComments> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QuestionsComments::getPid, commentId);
        return R.ok(fQuestionsCommentsService.list(wrapper));
    }

    //添加评论
    @PostMapping("/commentList")
    public R addComment(QuestionsComments questionsComments) {
        questionsComments.setId(null);
        return R.ok(fQuestionsCommentsService.save(questionsComments));
    }

    //点赞
    @RequestMapping("/like")
    public R like(Integer commentId,Boolean isLike) {
        LambdaQueryWrapper<QuestionsComments> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QuestionsComments::getId, commentId);
        if (isLike) {
            wrapper.inSql(null,"like_count=like_count-1");
        }else {
            wrapper.inSql(null,"like_count=like_count+1");
        }
        boolean update = fQuestionsCommentsService.update(wrapper);
        if (update) {
            return  R.ok("点赞成功");
        }else {
            return  R.err("点赞失败");
        }
    }
}
